1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.net;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 import static com.google.common.base.Preconditions.checkState;
22
23 import com.google.common.annotations.Beta;
24 import com.google.common.annotations.GwtCompatible;
25 import com.google.common.base.Objects;
26 import com.google.common.base.Strings;
27
28 import java.io.Serializable;
29
30 import javax.annotation.Nullable;
31 import javax.annotation.concurrent.Immutable;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 @Beta
65 @Immutable
66 @GwtCompatible
67 public final class HostAndPort implements Serializable {
68
69 private static final int NO_PORT = -1;
70
71
72 private final String host;
73
74
75 private final int port;
76
77
78 private final boolean hasBracketlessColons;
79
80 private HostAndPort(String host, int port, boolean hasBracketlessColons) {
81 this.host = host;
82 this.port = port;
83 this.hasBracketlessColons = hasBracketlessColons;
84 }
85
86
87
88
89
90
91
92
93 public String getHostText() {
94 return host;
95 }
96
97
98 public boolean hasPort() {
99 return port >= 0;
100 }
101
102
103
104
105
106
107
108
109 public int getPort() {
110 checkState(hasPort());
111 return port;
112 }
113
114
115
116
117 public int getPortOrDefault(int defaultPort) {
118 return hasPort() ? port : defaultPort;
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public static HostAndPort fromParts(String host, int port) {
134 checkArgument(isValidPort(port), "Port out of range: %s", port);
135 HostAndPort parsedHost = fromString(host);
136 checkArgument(!parsedHost.hasPort(), "Host has a port: %s", host);
137 return new HostAndPort(parsedHost.host, port, parsedHost.hasBracketlessColons);
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151 public static HostAndPort fromHost(String host) {
152 HostAndPort parsedHost = fromString(host);
153 checkArgument(!parsedHost.hasPort(), "Host has a port: %s", host);
154 return parsedHost;
155 }
156
157
158
159
160
161
162
163
164
165
166
167 public static HostAndPort fromString(String hostPortString) {
168 checkNotNull(hostPortString);
169 String host;
170 String portString = null;
171 boolean hasBracketlessColons = false;
172
173 if (hostPortString.startsWith("[")) {
174 String[] hostAndPort = getHostAndPortFromBracketedHost(hostPortString);
175 host = hostAndPort[0];
176 portString = hostAndPort[1];
177 } else {
178 int colonPos = hostPortString.indexOf(':');
179 if (colonPos >= 0 && hostPortString.indexOf(':', colonPos + 1) == -1) {
180
181 host = hostPortString.substring(0, colonPos);
182 portString = hostPortString.substring(colonPos + 1);
183 } else {
184
185 host = hostPortString;
186 hasBracketlessColons = (colonPos >= 0);
187 }
188 }
189
190 int port = NO_PORT;
191 if (!Strings.isNullOrEmpty(portString)) {
192
193
194 checkArgument(!portString.startsWith("+"), "Unparseable port number: %s", hostPortString);
195 try {
196 port = Integer.parseInt(portString);
197 } catch (NumberFormatException e) {
198 throw new IllegalArgumentException("Unparseable port number: " + hostPortString);
199 }
200 checkArgument(isValidPort(port), "Port number out of range: %s", hostPortString);
201 }
202
203 return new HostAndPort(host, port, hasBracketlessColons);
204 }
205
206
207
208
209
210
211
212
213 private static String[] getHostAndPortFromBracketedHost(String hostPortString) {
214 int colonIndex = 0;
215 int closeBracketIndex = 0;
216 checkArgument(hostPortString.charAt(0) == '[',
217 "Bracketed host-port string must start with a bracket: %s", hostPortString);
218 colonIndex = hostPortString.indexOf(':');
219 closeBracketIndex = hostPortString.lastIndexOf(']');
220 checkArgument(colonIndex > -1 && closeBracketIndex > colonIndex,
221 "Invalid bracketed host/port: %s", hostPortString);
222
223 String host = hostPortString.substring(1, closeBracketIndex);
224 if (closeBracketIndex + 1 == hostPortString.length()) {
225 return new String[] { host, "" };
226 } else {
227 checkArgument(hostPortString.charAt(closeBracketIndex + 1) == ':',
228 "Only a colon may follow a close bracket: %s", hostPortString);
229 for (int i = closeBracketIndex + 2; i < hostPortString.length(); ++i) {
230 checkArgument(Character.isDigit(hostPortString.charAt(i)),
231 "Port must be numeric: %s", hostPortString);
232 }
233 return new String[] { host, hostPortString.substring(closeBracketIndex + 2) };
234 }
235 }
236
237
238
239
240
241
242
243
244
245
246
247 public HostAndPort withDefaultPort(int defaultPort) {
248 checkArgument(isValidPort(defaultPort));
249 if (hasPort() || port == defaultPort) {
250 return this;
251 }
252 return new HostAndPort(host, defaultPort, hasBracketlessColons);
253 }
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270 public HostAndPort requireBracketsForIPv6() {
271 checkArgument(!hasBracketlessColons, "Possible bracketless IPv6 literal: %s", host);
272 return this;
273 }
274
275 @Override
276 public boolean equals(@Nullable Object other) {
277 if (this == other) {
278 return true;
279 }
280 if (other instanceof HostAndPort) {
281 HostAndPort that = (HostAndPort) other;
282 return Objects.equal(this.host, that.host)
283 && this.port == that.port
284 && this.hasBracketlessColons == that.hasBracketlessColons;
285 }
286 return false;
287 }
288
289 @Override
290 public int hashCode() {
291 return Objects.hashCode(host, port, hasBracketlessColons);
292 }
293
294
295 @Override
296 public String toString() {
297
298 StringBuilder builder = new StringBuilder(host.length() + 8);
299 if (host.indexOf(':') >= 0) {
300 builder.append('[').append(host).append(']');
301 } else {
302 builder.append(host);
303 }
304 if (hasPort()) {
305 builder.append(':').append(port);
306 }
307 return builder.toString();
308 }
309
310
311 private static boolean isValidPort(int port) {
312 return port >= 0 && port <= 65535;
313 }
314
315 private static final long serialVersionUID = 0;
316 }